from .SNMP import Wrpc
from .SNMP import Node
from .SNMP import Master
from .triger import *
import time
def device_biaoding(ip):
    print(Wrpc.wrpcVersionGroup().walk_wrpcVersionGroup(ip=ip))
    Dtx = 0
    Drx = 0
    impBPSConfig = Node.impBPSConfig()
    wrpcPtpGroup = Wrpc.wrpcPtpGroup()
    wrpcPtpConfigGroup = Wrpc.wrpcPtpConfigGroup()
    impWavePatternSetGroup = Node.impWavePatternSetGroup()
    impCtrlApply = Node.impCtrlApply()
    # Node.impCtrlApply().set_impCtrlApply(ip=ip,value=1)
    

    # Node.impCtrlMENA().set_impCtrlMC0Ena(ip=ip,value=1)
    # Node.impCtrlMENA().set_impCtrlMC1Ena(ip=ip,value=1)
    # Node.impCtrlMENA().set_impCtrlUtcCodingEna(ip=ip,value=0)
    # Node.impCtrlMENA().set_impCtrlInterlockEna(ip=ip,value=0)
    # Node.impCtrlMENA().set_impCtrlHeartbeatEna(ip=ip,value=0)

    # Node.impCtrlApply().set_impCtrlApply(ip=ip,value=11)
    # print(Node.impCtrlMENA().walk_impCtrlMENA(ip=ip))

    # impWavePatternSetGroup = Node.impWavePatternSetGroup()
    # impWavePatternSetGroup.set_impWavePatternIndex(ip,0)
    # impWavePatternSetGroup.set_impWavePatternMC(ip,0)
    # impWavePatternSetGroup.set_impWavePatternDelay(ip,0)
    # impWavePatternSetGroup.set_impWavePatternWidth(ip,2500000)
    # impWavePatternSetGroup.set_impWavePatternIdle4ns(ip,22500000)
    # impWavePatternSetGroup.set_impWavePatternIdle100ms(ip,9)
    # impWavePatternSetGroup.set_impWavePatternSet(ip,1)
    # Node.impCtrlApply().set_impCtrlApply(ip,3)
    # # print("修改MC0输出参数为PPS")

    # print(Node.impInterlockReadGroup().walk_impInterlockReadGroup(ip=ip))
    # impWaveTableMC0_data = list(Node.impWavePatternTable().walk_impWaveTableMC0(ip=ip).values())
    # print(impWaveTableMC0_data[0],impWaveTableMC0_data[16],impWaveTableMC0_data[32],impWaveTableMC0_data[48])

    # Node.impCtrlApply().set_impCtrlApply(ip=ip,value=3)

    wrpcPtpConfigGroup.set_wrpcPtpConfigSfpPn(ip=ip,value="GE-LC-1310      ")
    wrpcPtpConfigGroup.set_wrpcPtpConfigDeltaTx(ip=ip,value=Dtx)
    wrpcPtpConfigGroup.set_wrpcPtpConfigDeltaRx(ip=ip,value=Drx)
    wrpcPtpConfigGroup.set_wrpcPtpConfigAlphaL(ip=ip,value=73621684)
    wrpcPtpConfigGroup.set_wrpcPtpConfigAlphaH(ip=ip,value=0)
    wrpcPtpConfigGroup.set_wrpcPtpConfigApply(ip=ip,value=2)
    wrpcPtpConfigGroup.set_wrpcPtpConfigApply(ip=ip,value=3)
    print("初始化Ptp参数")

    print(Wrpc.wrpcSfpTable().walk_wrpcSfpTable(ip=ip))
    while True:
        time.sleep(1)
        tri_state = impBPSConfig.get_impCtrlTrigger(ip=ip)["impCtrlTrigger"]
        if tri_state=="11":
            print("\t连续触发")
            print("\t\t请重新插拔光纤")
            delay = int(input("\t\t\t请输出示波器延时读数(ps):"))
            mu = int(wrpcPtpGroup.get_wrpcPtpRTT(ip=ip)["wrpcPtpRTT"])
            rxm = int(wrpcPtpGroup.get_wrpcPtpDeltaRxM(ip=ip)["wrpcPtpDeltaRxM"])
            txm = int(wrpcPtpGroup.get_wrpcPtpDeltaTxM(ip=ip)["wrpcPtpDeltaTxM"])
            rxs = int(wrpcPtpGroup.get_wrpcPtpDeltaRxS(ip=ip)["wrpcPtpDeltaRxS"])
            txs = int(wrpcPtpGroup.get_wrpcPtpDeltaTxS(ip=ip)["wrpcPtpDeltaTxS"])
            print(mu,rxm,txm,rxs,txs)
            _init_value = int((mu-rxm-txm-rxs-txs-delay)/2)

            Dtx = _init_value - delay
            Drx = _init_value + delay
            print(f"\t\t\t\t设置Ptp参数:\n\t\t\t\t\tDTx:{Dtx}\tDRx:{Drx}")
            wrpcPtpConfigGroup.set_wrpcPtpConfigDeltaTx(ip=ip,value=Dtx)
            wrpcPtpConfigGroup.set_wrpcPtpConfigDeltaRx(ip=ip,value=Drx)
            wrpcPtpConfigGroup.set_wrpcPtpConfigApply(ip=ip,value=2)
            wrpcPtpConfigGroup.set_wrpcPtpConfigApply(ip=ip,value=3)

            print(Wrpc.wrpcSfpTable().walk_wrpcSfpTable(ip=ip))

            i = 1
            while True:
                time.sleep(1)
                print(f"\t开始第{i}次补偿:")
                print("\t\t请重新插拔光纤")
                delay = int(input("\t\t\t请输出示波器延时读数(ps):"))
                if abs(delay) < 20:
                    print("延时小于20ps,标定结束")
                    break
                Dtx -= delay
                Drx += delay
                print(f"\t\t\t\t设置Ptp参数:\n\t\t\t\t\tDTx:{Dtx}\tDRx:{Drx}")
                wrpcPtpConfigGroup.set_wrpcPtpConfigDeltaTx(ip=ip,value=Dtx)
                wrpcPtpConfigGroup.set_wrpcPtpConfigDeltaRx(ip=ip,value=Drx)
                wrpcPtpConfigGroup.set_wrpcPtpConfigApply(ip=ip,value=2)
                wrpcPtpConfigGroup.set_wrpcPtpConfigApply(ip=ip,value=3)
                #print(Wrpc.wrpcSfpTable().walk_wrpcSfpTable(ip=ip))
                
                i += 1
                
            break


    # impCtrlMENA.set_impCtrlInterlockEna(ip=ip,value=1)
    # impCtrlApply.set_impCtrlApply(ip=ip,value=11)
    
    impCtrlApply.set_impCtrlApply(ip,3)

    # try:
    #     impBPSConfig.set_impCtrlDeviceNum(ip=ip,value=impCtrlDeviceNum)
    #     impBPSConfig.set_impCtrlHeartGroup(ip=ip,value=impCtrlHeartGroup)
    #     impBPSConfig.set_impCtrlHeartCycle(ip=ip,value=impCtrlHeartCycle)
    #     impBPSConfig.set_impCtrlHeartOffset(ip=ip,value=impCtrlHeartOffset)
    #     impBPSConfig.set_impCtrlCarrier(ip=ip,value=impCtrlCarrier)

    #     impCtrlApply.set_impCtrlApply(ip=ip,value=15)
    # except:
    #     pass

def _biaoding(ip):
    # ip = str(input("请输入标定设备IP:"))
    device_biaoding(ip)